
import { _decorator, Component, instantiate, Node, Prefab, random, Vec2, Vec3 } from 'cc';
import { CamreaCtrl } from '../game/CamreraCtrl';
import { Tools } from '../utils/Tools';
import { Config, SkillType } from '../utils/Config';
import { JoyStickManager } from '../utils/JoyStickManager';
import { SkillPool } from './SkillPool';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = AiPoolCtrl
 * DateTime = Wed Feb 28 2024 17:25:25 GMT+0800 (中国标准时间)
 * Author = heyongtao
 * FileBasename = AiPoolCtrl.ts
 * FileBasenameNoExtension = AiPoolCtrl
 * URL = db://assets/scripts/ai/AiPoolCtrl.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
@ccclass('AiPoolCtrl')
export class AiPoolCtrl extends Component {
    // [1]
    // dummy = '';
    @property(Node)
    player : Node = null;
    @property(Prefab)
    ai : Prefab = null;
    @property(Prefab)
    bullet : Prefab = null;
    @property(Prefab)
    particle : Prefab = null;
    //skill
    @property(Prefab)
    surroundSkill : Prefab = null;
    @property(Prefab)
    trackSkill : Prefab = null;
    // ai
    private allAi : Node[] = []
    private aiCreateNumPer : number = 5;
    
    getAllAi()
    {
        return this.allAi;
    }

    getRandomCreateAi()
    {
        for(let k=0;k<this.aiCreateNumPer;++k)
        {
            let cameraPos = CamreaCtrl.GetInstance().getCameraPos();
            let aiNode : Node = instantiate(this.ai);
            this.node.addChild(aiNode);
            // 基于当前视野范围内从四角随机位置
            let typePos = Tools.GetInstance().getRandomInt(1,4);
            if(typePos == 1) //左上角
            {
                let right = cameraPos.x - Config.GetInstance().GameWinX/2 + Config.GetInstance().MapCornerRect;
                let left = right - Config.GetInstance().MapCornerRect*2;
                let randomX = Tools.GetInstance().getRandomInt(left,right);
                let randomY = Tools.GetInstance().getRandomInt(0,Config.GetInstance().MapCornerRect);
                randomY = cameraPos.y + Config.GetInstance().GameWinY/2 - randomY;
                aiNode.setPosition(new Vec3(randomX,randomY,0));
            }
            else if(typePos ==2 ) // 左下角
            {
                let right = cameraPos.x - Config.GetInstance().GameWinX/2 + Config.GetInstance().MapCornerRect;
                let left = right - Config.GetInstance().MapCornerRect*2;
                let randomX = Tools.GetInstance().getRandomInt(left,right);
                let randomY = Tools.GetInstance().getRandomInt(0,Config.GetInstance().MapCornerRect);
                randomY = cameraPos.y - Config.GetInstance().GameWinY/2 + randomY;
                aiNode.setPosition(new Vec3(randomX,randomY,0));
            }
            else if(typePos == 3) // 右上角
            {
                let right = cameraPos.x + Config.GetInstance().GameWinX/2 + Config.GetInstance().MapCornerRect;
                let left = right - Config.GetInstance().MapCornerRect*2;
                let randomX = Tools.GetInstance().getRandomInt(left,right);
                let randomY = Tools.GetInstance().getRandomInt(0,Config.GetInstance().MapCornerRect);
                randomY = cameraPos.y + Config.GetInstance().GameWinY/2 - randomY;
                aiNode.setPosition(new Vec3(randomX,randomY,0));
            }
            else if(typePos == 4) // 右上角
            {
                let right = cameraPos.x + Config.GetInstance().GameWinX/2 + Config.GetInstance().MapCornerRect;
                let left = right - Config.GetInstance().MapCornerRect*2;
                let randomX = Tools.GetInstance().getRandomInt(left,right);
                let randomY = Tools.GetInstance().getRandomInt(0,Config.GetInstance().MapCornerRect);
                randomY = cameraPos.y - Config.GetInstance().GameWinY/2 + randomY;
                aiNode.setPosition(new Vec3(randomX,randomY,0));
            }
            aiNode.getComponent("AiCtrl").setPlayer(this.player);
            this.allAi.push(aiNode);            
        }

    }

    // getCreateBullet()
    // {
    //     let dir : Vec3 = JoyStickManager.GetInstance().joyStick.getDir();
    //     let normalizedDirection = new Vec2(0,0);
    //     Vec2.normalize(normalizedDirection,dir);
    //     if(normalizedDirection.x == 0 && normalizedDirection.y == 0) 
    //     {
    //         normalizedDirection.x = normalizedDirection.y = 1;
    //     }
    //     let initPos = this.player.getPosition();
    //     const moveDistance = this.bulletInitSpeed * 0.015;
    //     const scaledDirection = new Vec2(moveDistance*normalizedDirection.x,moveDistance*normalizedDirection.y);
    //     initPos.x += scaledDirection.x;
    //     initPos.y += scaledDirection.y;
        
    //     // create 
    //     let bulletNode : Node = instantiate(this.bullet);
    //     bulletNode.setPosition(new Vec3(initPos.x,initPos.y + 20,0));
    //     // particle 
    //     this.node.addChild(bulletNode);
    //     bulletNode.getComponent("BulletCtrl").setDirection(normalizedDirection);
    //     bulletNode.getComponent("BulletCtrl").setUniqueId(this.bulletUniqueId);
    //     this.bulletUniqueId += 1;
    // }

    protected onLoad(): void {
        SkillPool.GetInstance().setParent(this.node);
    }

    start () {
        // [3]
        this.schedule(this.getRandomCreateAi,1.0);
        //this.schedule(this.getCreateBullet,this.createBulletSpeed);
        SkillPool.GetInstance().CreateSkill(SkillType.SurroundSkill,this.surroundSkill,this.player);
        SkillPool.GetInstance().CreateSkill(SkillType.BulletSkill,this.bullet,this.player);
        SkillPool.GetInstance().CreateSkill(SkillType.TrackSkill,this.trackSkill,this.player);
    }

    private checkLiveAi()
    {
        this.allAi = this.allAi.filter(node => node !== null);  
    }

    update (deltaTime: number) {
        SkillPool.GetInstance().update(deltaTime);
        this.checkLiveAi();
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
